package Day2025_04_12;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo09 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Integer> list = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            list.add(a);
        }
        while (list.get(list.size() - 1) == list.size()) {
            list.remove(list.size() - 1);
        }
        int nn = list.size();
        int i = 0;
        for ( ; i < nn; i++) {
            if (list.get(i) == nn) break;
        }
        int ret = (1 + nn) * nn / 2 - i - i - 2;
        System.out.println(ret);
    }
}
